##### Replication Figures 18 and 19

rm(list = ls())
library(tidyverse)
library(igraph)
library(ggraph)
library(data.table)
library(ggplot2)

cenl <- read_csv("nltrial_clean.csv")
ceelall <- read_csv("eltrial_clean.csv")

# who are the people nominated by the individuals of different waves: 
el <- ceelall %>% filter(source %in% cenl$ID[cenl$wave==1])
mydt<- el %>% group_by(target) %>% summarise(indegree=n())
names(mydt)[1] <- "ID"
mydt$totindegree <- mydt$indegree
mydt$idrank <- as.numeric(as.factor(-mydt$indegree))
mydt$totidrank <- as.numeric(as.factor(-mydt$totindegree))
mydt <- mydt %>% mutate(idrankmin=min_rank(-indegree))
mydt <- mydt %>% mutate(totidrankmin=min_rank(-totindegree))
mydt$wave <- 1
mydt$pdegree <- mydt$indegree/sum(mydt$indegree)
mydt$ptotdegree <- mydt$totindegree/sum(mydt$totindegree)

mydtnon <- cenl %>% select(ID)
mydtnon$indegree <- 0
mydtnon$totindegree <- 0
mydtnon$idrank <- NA
mydtnon$totidrank <- NA
mydtnon$idrankmin <- NA
mydtnon$totidrankmin <- NA
mydtnon$wave <- NA
mydtnon$pdegree <- 0
mydtnon$ptotdegree <- 0

wavedt <- rbind(mydt, mydtnon %>% filter(!(ID %in% mydt$ID)))


for(i in unique(cenl$wave[cenl$wave!=0 & cenl$wave!=Inf])){
  el <- ceelall %>% filter(source %in% cenl$ID[cenl$wave==i])
  mydt <- el %>% group_by(target) %>% summarise(indegree=n())
  names(mydt)[1] <- "ID"
  el <- ceelall %>% filter(source %in% cenl$ID[cenl$wave<=i])
  mydttot <- el %>% group_by(target) %>% summarise(totindegree=n())
  names(mydttot)[1] <- "ID"
  mydt <- left_join(mydttot, mydt, by="ID")
  mydt$indegree[is.na(mydt$indegree)] <- 0
  mydt$idrank <- as.numeric(as.factor(-mydt$indegree))
  mydt$totidrank <- as.numeric(as.factor(-mydt$totindegree))
  mydt <- mydt %>% mutate(idrankmin=min_rank(-indegree))
  mydt <- mydt %>% mutate(totidrankmin=min_rank(-totindegree))
  mydt$wave <- i
  mydt$pdegree <- mydt$indegree/sum(mydt$indegree)
  mydt$ptotdegree <- mydt$totindegree/sum(mydt$totindegree)
  
  mydt <- rbind(mydt, mydtnon %>% filter(!(ID %in% mydt$ID)))
  wavedt <- rbind(wavedt, mydt)
  rm(el, mydt, mydttot)
  print(i)
}


#### replication figure 18 ####

tops <- c(10, 25, 50, 75, 100, 250, 500)

topdt2 <- cbind(rep(tops, 11), rep(1:11, each=7)) %>% as_tibble()
names(topdt2) <- c("top", "wave")
topdt2$pintop <- NA


cutoff <- wavedt %>% filter(wave==1) %>% arrange(-totindegree)
top10 <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[10]]
top25 <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[25]]
top50 <- top25
top75 <- top25
top100 <- top25
top250 <- top25
top500 <- top25

for(i in 1:11){
  cutoff <- wavedt %>% filter(wave==i) %>% arrange(-totindegree)
  top10b <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[10]]
  topdt2$pintop[topdt2$top==10 & topdt2$wave==i] <- sum(top10b %in% top10)/length(top10b)
  top25b <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[25]]
  topdt2$pintop[topdt2$top==25 & topdt2$wave==i] <- sum(top25b %in% top25)/length(top25b)
  top50b <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[50]]
  topdt2$pintop[topdt2$top==50 & topdt2$wave==i] <- sum(top50b %in% top50)/length(top50b)
  top75b <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[75]]
  topdt2$pintop[topdt2$top==75 & topdt2$wave==i] <- sum(top75b %in% top75)/length(top75b)
  top100b <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[100]]
  topdt2$pintop[topdt2$top==100 & topdt2$wave==i] <- sum(top100b %in% top100)/length(top100b)
  top250b <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[250]]
  topdt2$pintop[topdt2$top==250 & topdt2$wave==i] <- sum(top250b %in% top250)/length(top250b)
  top500b <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[500]]
  topdt2$pintop[topdt2$top==500 & topdt2$wave==i] <- sum(top500b %in% top500)/length(top500b)
  
  top10 <- top10b
  top25 <- top25b
  top50 <- top50b
  top75 <- top75b
  top100 <- top100b
  top250 <- top250b
  top500 <- top500b
  print(i)
}

cutoff <- wavedt %>% filter(wave==1) %>% arrange(-totindegree)
top500 <- cutoff$ID

cutoff <- wavedt %>% filter(wave==2) %>% arrange(-totindegree)
top500b <- cutoff$ID[cutoff$totindegree>=cutoff$totindegree[500]]

topdt2$pintop[topdt2$top==500 & topdt2$wave==2] <- sum(top500b %in% top500)/length(top500b)


# indegree of top 125 over wave:
(ps_stability <- topdt2 %>% 
    filter(wave>1 & top %in% c(10, 100, 500)) %>% 
    mutate(topstr = paste0("Top ", top)) %>%
    ggplot(aes(x = wave, y = pintop, color = as.factor(topstr))) +
    geom_line(linewidth=2) +
    geom_point(size=3) +
    labs(color = "", y = "percentage", x = "wave") +
    theme_minimal() + 
    scale_x_continuous(breaks = 2:11)  +
    scale_color_grey()) +
  ggtitle("Stability of top China Watchers between each wave")

##### Replication Figure 19 ###

table(cenl$wave, useNA="always")

mydt <- c("50+", "40+", "30+", "20+", "15+", "10+") %>% as_tibble()
names(mydt) <- "top"

el <- ceelall %>% filter(source %in% cenl$ID[cenl$wave==1])

mydt$intop[mydt$top=="50+"] <- sum(el$target %in% cenl$ID[cenl$indegree>49])
mydt$pintop[mydt$top=="50+"] <- sum(el$target %in% cenl$ID[cenl$indegree>49])/length(el$target)

mydt$intop[mydt$top=="40+"] <- sum(el$target %in% cenl$ID[cenl$indegree>39])
mydt$pintop[mydt$top=="40+"] <- sum(el$target %in% cenl$ID[cenl$indegree>39])/length(el$target)

mydt$intop[mydt$top=="30+"] <- sum(el$target %in% cenl$ID[cenl$indegree>29])
mydt$pintop[mydt$top=="30+"] <- sum(el$target %in% cenl$ID[cenl$indegree>29])/length(el$target)

mydt$intop[mydt$top=="20+"] <- sum(el$target %in% cenl$ID[cenl$indegree>19])
mydt$pintop[mydt$top=="20+"] <- sum(el$target %in% cenl$ID[cenl$indegree>19])/length(el$target)

mydt$intop[mydt$top=="15+"] <- sum(el$target %in% cenl$ID[cenl$indegree>14])
mydt$pintop[mydt$top=="15+"] <- sum(el$target %in% cenl$ID[cenl$indegree>14])/length(el$target)


mydt$intop[mydt$top=="10+"] <- sum(el$target %in% cenl$ID[cenl$indegree>9])
mydt$pintop[mydt$top=="10+"] <- sum(el$target %in% cenl$ID[cenl$indegree>9])/length(el$target)

mydt$wave <- 1

topdt <- mydt

for(i in unique(cenl$wave[cenl$wave!=0 & cenl$wave!=Inf])){
  mydt <- c("50+", "40+", "30+", "20+", "15+", "10+") %>% as_tibble()
  names(mydt) <- "top"
  
  el <- ceelall %>% filter(source %in% cenl$ID[cenl$wave==i])
  
  mydt$intop[mydt$top=="50+"] <- sum(el$target %in% cenl$ID[cenl$indegree>49])
  mydt$pintop[mydt$top=="50+"] <- sum(el$target %in% cenl$ID[cenl$indegree>49])/length(el$target)
  
  mydt$intop[mydt$top=="40+"] <- sum(el$target %in% cenl$ID[cenl$indegree>39])
  mydt$pintop[mydt$top=="40+"] <- sum(el$target %in% cenl$ID[cenl$indegree>39])/length(el$target)
  
  mydt$intop[mydt$top=="30+"] <- sum(el$target %in% cenl$ID[cenl$indegree>29])
  mydt$pintop[mydt$top=="30+"] <- sum(el$target %in% cenl$ID[cenl$indegree>29])/length(el$target)
  
  mydt$intop[mydt$top=="20+"] <- sum(el$target %in% cenl$ID[cenl$indegree>19])
  mydt$pintop[mydt$top=="20+"] <- sum(el$target %in% cenl$ID[cenl$indegree>19])/length(el$target)
  
  mydt$intop[mydt$top=="15+"] <- sum(el$target %in% cenl$ID[cenl$indegree>14])
  mydt$pintop[mydt$top=="15+"] <- sum(el$target %in% cenl$ID[cenl$indegree>14])/length(el$target)
  
  
  mydt$intop[mydt$top=="10+"] <- sum(el$target %in% cenl$ID[cenl$indegree>9])
  mydt$pintop[mydt$top=="10+"] <- sum(el$target %in% cenl$ID[cenl$indegree>9])/length(el$target)
  
  mydt$wave <- i
  
  topdt <- rbind(topdt, mydt)
  rm(el, mydt)
  print(i)
}


# indegree of top 125 over wave:
topdt %>% 
  #filter(wave>0) %>%
  ggplot(aes(x = wave, y = pintop, color = as.factor(top))) +
  geom_line() +
  ggtitle("Percentage of nominations going to top China Watchers in each wave") +
  labs(color = "top china watchers\n(nominations received)") +
  ylab("percentage of nominations")

